<script setup lang="ts">
import { ref, computed } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useEventManage } from "./utils/hook";
import AddFill from "~icons/ri/add-circle-line";
import Refresh from "~icons/ep/refresh";
import EditPen from "~icons/ep/edit-pen";
import Delete from "~icons/ep/delete";
import More from "~icons/ep/more-filled";

defineOptions({
  name: "EventManage"
});

const activeTab = ref(0);
const tableRef = ref();
const formRef = ref();

type TabType = "eventList" | "triggerConditions" | "executeActions";
const tabs = [
  { name: "事件列表", key: "eventList" },
  { name: "触发条件", key: "triggerConditions" },
  { name: "执行动作", key: "executeActions" }
];

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  selectedNum,
  buttonClass,
  deviceDetection,
  onSearch,
  resetForm,
  onbatchDel,
  openDialog,
  handleUpdate,
  handleDelete,
  handleSizeChange,
  onSelectionCancel,
  handleCurrentChange,
  handleSelectionChange
} = useEventManage(tableRef);

// 根据当前选中的标签获取对应的列配置
const currentColumns = computed(() => {
  return columns[activeTab.value];
});

// 根据当前选中的标签获取对应的数据
const currentDataList = computed(() => {
  return dataList[activeTab.value];
});
</script>

<template>
  <div class="main-content">
    <el-card class="mb-4">
      <el-tabs v-model="activeTab" class="mb-4">
        <el-tab-pane
          v-for="(tab, index) in tabs"
          :key="tab.key"
          :label="tab.name"
          :name="index"
        />
      </el-tabs>

      <!-- 搜索表单 -->
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item
          v-if="activeTab === 0"
          label="事件名称："
          prop="eventName"
        >
          <el-input
            v-model="form.eventName"
            placeholder="请输入事件名称"
            clearable
            class="w-[180px]!"
          />
        </el-form-item>
        <el-form-item v-if="activeTab === 0" label="事件ID:" prop="eventId">
          <el-input
            v-model="form.eventId"
            placeholder="请输入事件ID"
            clearable
            class="w-[180px]!"
          />
        </el-form-item>
        <el-form-item v-if="activeTab === 0" label="状态：" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择"
            clearable
            class="w-[180px]!"
          >
            <el-option label="已启用" value="1" />
            <el-option label="已禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 表格工具栏 -->
      <PureTableBar
        :title="tabs[activeTab].name"
        :columns="currentColumns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog()"
          >
            新增
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <div
            v-if="selectedNum > 0"
            v-motion-fade
            class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
          >
            <div class="flex-auto">
              <span
                style="font-size: var(--el-font-size-base)"
                class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
              >
                已选 {{ selectedNum }} 项
              </span>
              <el-button type="primary" text @click="onSelectionCancel">
                取消选择
              </el-button>
            </div>
            <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
              <template #reference>
                <el-button type="danger" text class="mr-1!">
                  批量删除
                </el-button>
              </template>
            </el-popconfirm>
          </div>
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="currentDataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <el-popconfirm
                :title="`是否确认删除ID为${row.id}的这条数据`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
              <el-dropdown>
                <el-button
                  class="ml-3! mt-[2px]!"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(More)"
                  @click="handleUpdate(row)"
                />
                <template #dropdown>
                  <el-dropdown-menu>
                    <!-- 根据不同标签显示不同的操作选项 -->
                    <el-dropdown-item v-if="activeTab === 0">
                      <el-button
                        :class="buttonClass"
                        link
                        type="primary"
                        :size="size"
                        @click="handleUpdate(row)"
                      >
                        查看详情
                      </el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {};
</script>
<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
